package vmcs.boundary;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto, Pang Pingli
//
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import vmcs.controller.CustomerController;
import vmcs.controller.MaintenanceController;
import vmcs.controller.StoreController;
import vmcs.utility.MoneyFormatter;
import vmcs.utility.VMCSException;

public class CustomerPanel extends Dialog {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public final static char WORKING = 'a';
    public final static char PSWD = 'b';
    public final static char DIALOG = 'c';
    private static final String TITLE = "Customer Panel";
    private CoinDisplayCustomer cDisplay; // need to be access from other class.
    private DrinkDisplayCustomer dDisplay;  // need to be access from other class.
    private ButtonItem totalCash;
    private Button transferCash;
    private CustomerController mctrl;

    public CustomerPanel(Frame fr, CustomerController mc, MaintenanceController maintController) {
        super(fr, TITLE, false);

        mctrl = mc;

        // north part
        Label lb = new Label(TITLE);
        lb.setFont(new Font("Helvetica", Font.BOLD, 24));
        Panel tp1 = new Panel();
        tp1.add(lb);

        Panel tpn = new Panel();
        tpn.setLayout(new GridLayout(0, 1));

        Panel tp3 = new Panel();
        tpn.add(tp1);
        tpn.add(tp3);
        tp3.setLayout(new BoxLayout(tp3, BoxLayout.X_AXIS));

        cDisplay = new CoinDisplayCustomer(mctrl, maintController);
        tp3.add(cDisplay);

        // center part
        Panel tpc = new Panel();
        tpc.setLayout(new GridLayout(0, 1));

        Panel tp5 = new Panel();
        tp5.setLayout(new GridLayout(0, 1));

        // price = new LabelledDisplay("Brand Price", 5, LabelledDisplay.FLOW);
        totalCash = new ButtonItem("Show Total Cash Held", ButtonItem.FLOW);

        transferCash = new Button("Terminate and Return Cash");

        Panel tp6 = new Panel();
        tp6.setLayout(new FlowLayout());
        tp6.add(transferCash);

        //tp5.add(totalCash);
        dDisplay = new DrinkDisplayCustomer(mctrl, maintController);
        totalCash.add(dDisplay);
        tp5.add(tp6);

        Panel tpBtn = new Panel();

        tp5.add(tpBtn);
        tpc.setLayout(new BorderLayout());
        Panel pp = new Panel();
        pp.setLayout(new GridLayout(1, 2));

        JPanel panel = new JPanel();
        pp.add(panel);
        panel.setLayout(null);

        JButton btnNewButton = new JButton("Invalid Coin");
        btnNewButton.setBounds(188, 5, 89, 24);
        btnNewButton.setEnabled(false);
        btnNewButton.setBackground(Color.BLACK);
        btnNewButton.setForeground(Color.RED);
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(142, 55, 201, 24);
        panel.add(panel_1);

        JLabel lblNewLabel = new JLabel("Total Money Inserted        ");
        panel_1.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("0 C");
        panel_1.add(lblNewLabel_1);
        lblNewLabel_1.setForeground(Color.YELLOW);
        lblNewLabel_1.setBackground(Color.BLACK);
        // tpc.add(cDisplay);
        // tpc.add(dDisplay);
        tpc.add("Center", pp);
        tpc.add("South", tp5);

        this.setLayout(new BorderLayout());
        this.add("North", tpn);
        this.add("Center", tpc);

        fr.setLocation(100, 100);
        fr.pack();
        fr.setVisible(true);

        pack();
        setLocation(200, 100);


        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
            }
        });

    }

    public void display() {
        System.out.println("MaintenancePanel: before display");
        this.setVisible(true);
        System.out.println("MaintenancePanel: after display");

    }

    public void closeDown() {
        dispose();
    }

    public CoinDisplayCustomer getCoinDisplay() {
        return cDisplay;
    }

    public DrinkDisplayCustomer getDrinksDisplay() {
        return dDisplay;
    }

    public void setActive(char comp, boolean st) {
        switch (comp) {
            case DIALOG:
                setActive(PSWD, st);
                setActive(WORKING, !st);
                break;
            case WORKING:
                //collectCash.setActive(st);
                cDisplay.setActive(st);
                dDisplay.setActive(st);
                totalCash.setActive(st);
                transferCash.setEnabled(st);
                //exitBtn.setEnabled(st);
                break;
            case PSWD:
                break;
        }
    }

    public int getCurIdx() {
        return dDisplay.getCurIdx();
    }

    public void displayTotalCash(int tc) {
        String stc;

        stc = MoneyFormatter.toMoney(tc);
        totalCash.setValue(stc);
    }

    public void displayCoins(int cc) {
        //collectCash.setValue(cc);
    }

    /**
     *  use when machinery simulator panel changes qty;
     *  It is used to automatically update the displayed quantity in maintenance panel.
     *  It is called by Maintenance Controller
     *  Not required in requirement.
     */
    public void updateQtyDisplay(char type, int idx, int qty)
            throws VMCSException {
        if (type == StoreController.CASH) {
            if (cDisplay.getCurIdx() == idx) {
                cDisplay.displayQty(idx, qty);
            }
        } else {
            if (dDisplay.getCurIdx() == idx) {
                dDisplay.displayQty(idx, qty);
            }
        }
    }

    /**
     * When transfer all button is pushed, the current display needs to be updated.
     * not required in requirement.
     */
    public void updateCurrentQtyDisplay(char type, int qty)
            throws VMCSException {
        int curIdx;
        if (type == StoreController.CASH) {
            curIdx = cDisplay.getCurIdx();
        } else {
            curIdx = dDisplay.getCurIdx();
        }
        updateQtyDisplay(type, curIdx, qty);
    }

    public void initCollectCash() {
        //collectCash.setValue("");
    }

    public void initTotalCash() {
        totalCash.setValue("");
    }

    public void displayPrice(int price) {
        dDisplay.getPriceDisplay().setValue(MoneyFormatter.toMoney(price));
    }
}
